package com.tl.commonlibrary.storage.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.tl.commonlibrary.storage.database.dao.DaoMaster;
import com.tl.commonlibrary.storage.database.dao.DaoSession;
import com.tl.commonlibrary.storage.database.dao.UserDao;

/**
 * Created by Administrator on 2017/6/5 0005.
 */

public class DBManager {
    private volatile static DBManager manager;
    private DaoSession session;

    private DBManager() {

    }

    public static DBManager get() {
        if (manager == null) {
            synchronized (DBManager.class) {
                if (manager == null) {
                    manager = new DBManager();
                }
            }
        }
        return manager;
    }

    public void init(Context context, String dbName) {
        if(session != null){
            return;
        }
        OpenHelper helper = new OpenHelper(context, dbName);
        SQLiteDatabase db = helper.getWritableDatabase();
        DaoMaster master = new DaoMaster(db);
        session = master.newSession();
    }

    public static UserDao getUserDao() {
        if (manager.session != null) {
            return manager.session.getUserDao();
        }
        return null;
    }
}
